package com.example.loginmvvm.controller

import android.text.TextUtils
import com.example.loginmvvm.interfaces.OnCheckUserNameStateResultCallback
import com.example.loginmvvm.interfaces.OnDoLoginStateChange
import com.example.loginmvvm.lifecycle.ILifecycle
import com.example.loginmvvm.lifecycle.ILifecycleOwner
import com.example.loginmvvm.model.UserModel

class LoginPresenter {

    private val userModel by lazy {
        UserModel()
    }

    fun checkUserNameState(user: String, callback: OnCheckUserNameStateResultCallback) {
        /*if (user == "admin") {
            callback.onExist()
        } else {
            callback.onNotExist()
        }*/
        userModel.checkUserState(user) {
            when (it) {
                0 -> {
                    callback.onExist()
                }
                1 -> {
                    callback.onNotExist()
                }
            }
        }
    }

    fun doLogin(user: String, pass: String, callback: OnDoLoginStateChange) {

        //检查账号格式是否正确
        if (TextUtils.isEmpty(user)) {
            callback.onAccountFormatError()
            return
        }
        //检查密码长度是否正确
        if (TextUtils.isEmpty(pass)) {
            callback.onPasswordEmpty()
            return
        }

        /*if (user == "admin" && pass == "123456") {
            callback.onLoginSuccess()
        } else {
            callback.onLoginFailed()
        }*/
        userModel.doLogin(user, pass) {
            when (it) {
                UserModel.START_LOGIN_LOADING -> {
                    callback.onLogin()
                }

                UserModel.START_LOGIN_SUCCESS -> {
                    callback.onLoginSuccess()
                }

                UserModel.START_LOGIN_FAILED -> {
                    callback.onLoginFailed()
                }
            }
        }
    }
}